home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Blender 2.49b / blender-2.49b-windows.exe / $_4_ / .blender / scripts / uvcopy.py < prev    next >
Encoding:
Python Source  |  2009-08-31  |  3.3 KB  |  112 lines

  1. #!BPY
  2. """ Registration info for Blender menus: <- these words are ignored
  3. Name: 'UV Copy from Active'
  4. Blender: 242
  5. Group: 'Object'
  6. Tip: 'Copy UV coords from a mesh to another that has same vertex indices'
  7. """
  8.  
  9. __author__ = "Toni Alatalo, Martin Poirier et. al."
  10. __url__ = ("blender", "blenderartists.org")
  11. __version__ = "0.2 01/2006"
  12.  
  13. __bpydoc__ = """\
  14. This script copies UV coords from a mesh to another (version of the same mesh).
  15. All target meshes must have the same number of faces at the active
  16. """
  17.  
  18. # ***** BEGIN GPL LICENSE BLOCK *****
  19. #
  20. # This program is free software; you can redistribute it and/or
  21. # modify it under the terms of the GNU General Public License
  22. # as published by the Free Software Foundation; either version 2
  23. # of the License, or (at your option) any later version.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28. # GNU General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU General Public License
  31. # along with this program; if not, write to the Free Software Foundation,
  32. # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  33. #
  34. # ***** END GPL LICENCE BLOCK *****
  35.  
  36. import Blender
  37.  
  38. def face_key(me):
  39.     '''
  40.     Returns the face lengths for this mesh
  41.     only copy between meshes that have matching face_key's
  42.     '''
  43.     return tuple([len(f) for f in me.faces])
  44.  
  45. def main():
  46.     scene = Blender.Scene.GetCurrent()
  47.  
  48.     source_ob = scene.objects.active
  49.     
  50.     if not source_ob or source_ob.type != 'Mesh':
  51.         Blender.Draw.PupMenu("Error%t|No active object to copy UVs from.")
  52.         return
  53.     
  54.     source_me = source_ob.getData(mesh=1)
  55.     
  56.     target_mes = [ ob.getData(mesh=1) for ob in scene.objects.context if ob != source_ob if ob.type == 'Mesh']
  57.     
  58.     # remove double meshes
  59.     target_mes = dict([(me.name, me) for me in target_mes])
  60.     target_mes = target_mes.values()
  61.     
  62.     if not target_mes:
  63.         Blender.Draw.PupMenu("Error%t|No selected object(s) to copy UVs to.")
  64.         return
  65.     
  66.     source_me = source_ob.getData(mesh=1)
  67.     
  68.     if not source_me.faceUV:
  69.         Blender.Draw.PupMenu("Error%t|Active mesh has no UVs.")
  70.         return
  71.     
  72.     if not target_mes:
  73.         Blender.Draw.PupMenu("Error%t|no selected object other than the source, hence no target defined.")
  74.         return
  75.     error = False
  76.     
  77.     source_face_key = face_key(source_me)
  78.     source_faces = source_me.faces
  79.     
  80.     fail_count = 0
  81.     
  82.     for target in target_mes:
  83.         if len(source_me.faces) != len(target.faces):
  84.             print '\ttarget "%s" facelen does not match "%s".' % (target.name, source_me.name)
  85.             error = True
  86.             fail_count +=1
  87.         else:
  88.             # slow but worth comparing
  89.             if source_face_key != face_key(target):
  90.                 print '\t\terror, face len dosnt match for ob "%s" next mesh...' % target.name
  91.                 error = True
  92.                 fail_count +=1
  93.             else:
  94.                 target_faces = target.faces
  95.                 
  96.                 # Add uvs if there not there
  97.                 target.faceUV = True
  98.                 
  99.                 for i, f_source in enumerate(source_faces): 
  100.                     target_faces[i].uv = f_source.uv
  101.                 
  102.                 target.update()
  103.     
  104.     msg = 'Copied UVs to %d of %d mesh(s)' % (len(target_mes)-fail_count, len(target_mes))
  105.     
  106.     if error:
  107.         msg += "|Could not copy some UV's, see console."
  108.     
  109.     Blender.Draw.PupMenu('UV Copy Finished%t|' + msg)
  110.  
  111. if __name__ == '__main__':
  112.     main()